package cn.Liujialin.pet.service.impl;


import cn.Liujialin.basic.service.impl.BeanServiceImpl;
import cn.Liujialin.basic.util.DistanceUtil;
import cn.Liujialin.basic.util.PageList;
import cn.Liujialin.basic.util.Point;
import cn.Liujialin.org.domain.Employee;
import cn.Liujialin.org.domain.Shop;
import cn.Liujialin.org.mapper.EmployeeMapper;
import cn.Liujialin.org.mapper.ShopMapper;
import cn.Liujialin.pet.domain.Pet;
import cn.Liujialin.pet.domain.SearchMasterMsg;
import cn.Liujialin.pet.mapper.PetDetailMapper;
import cn.Liujialin.pet.mapper.PetMapper;
import cn.Liujialin.pet.mapper.SearchMasterMsgMapper;
import cn.Liujialin.pet.query.SearchMasterMsgQuery;
import cn.Liujialin.pet.service.IPetService;
import cn.Liujialin.pet.service.ISearchMasterMsgService;
import cn.Liujialin.user.domain.LoginInfo;
import cn.Liujialin.user.domain.User;
import cn.Liujialin.user.mapper.UserMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class SearchMasterMsgServiceImpl extends BeanServiceImpl<SearchMasterMsg> implements ISearchMasterMsgService {
    @Autowired
    private UserMapper userMapper;
    @Autowired
    private ShopMapper shopMapper;
    @Autowired
    private SearchMasterMsgMapper searchMasterMsgMapper;
    @Autowired
    private EmployeeMapper employeeMapper;
    @Autowired
    private PetMapper petMapper;
    @Autowired
    private PetDetailMapper petDetailMapper;
    @Autowired
    private IPetService petService;



   //保存寻主消息
    @Override
    public void publish(SearchMasterMsg searchMasterMsg, Long loginInfoId) {
        //获取当前登录人
        //导入工具类LoginContext 能够帮我们获取到当前登录人士谁
        User user = userMapper.findByLoginInfoId(loginInfoId);
        searchMasterMsg.setUser_id(user.getId());
        //获取最近的门店
        //DistanceUtil百度地图提供的工具类
        //Point自定义用来存放精度和维度
        Point point = DistanceUtil.getPoint(searchMasterMsg.getAddress());
        Shop nearestShop = DistanceUtil.getNearestShop(point, shopMapper.findByAll());
        searchMasterMsg.setShop_id(nearestShop.getId());
        //保存寻主消息
        searchMasterMsgMapper.save(searchMasterMsg);
        //给店长发送消息
        System.out.println("你的店铺收到"+searchMasterMsg.getName()+"收购信息，请及时上门收购");

    }
    //处理寻主消息
    @Override
    public void handle(Pet pet, LoginInfo currentLoginInfo) {
        //修改后的一行代码完成,getSearch_master_msg_id把private Long adopt_id;的adopt_id改了,y运用于一行保存
        searchMasterMsgMapper.updateMsgState4ProcessedById(pet.getSearch_master_msg_id());
        //获取当前操作人所属店铺
        Employee employee = employeeMapper.findByLoginInfoId(currentLoginInfo.getId());
        //宠物设置店铺
        pet.setShop_id(employee.getShop_id());
        //保存数据
        petService.save(pet);


    }

    //查询个人消息
    //待处理消息
    @Override
    public PageList<SearchMasterMsg> queryUserMsg(SearchMasterMsgQuery query, LoginInfo currentLoginInfo) {
        //获取当前登录人
        User user = userMapper.findByLoginInfoId(currentLoginInfo.getId());
        query.setUserId(user.getId());
        return super.queryPage(query);

    }
    //已处理消息
    @Override
    public PageList<SearchMasterMsg> processed(SearchMasterMsgQuery query, LoginInfo currentLoginInfo) {
        //已处理消息
        query.setState(0);
        //获取当前登录人
        Employee employee = employeeMapper.findByLoginInfoId(currentLoginInfo.getId());
        if(employee.getShop_id() != null){
            query.setShopId(employee.getShop_id());
        }
        return super.queryPage(query);
    }
    //待处理消息
    @Override
    public PageList<SearchMasterMsg> pending(SearchMasterMsgQuery query, LoginInfo currentLoginInfo) {
        //待处理消息
        query.setState(0);
        //获取当前登录人，这里和user一模一样的mapper
        Employee employee = employeeMapper.findByLoginInfoId(currentLoginInfo.getId());
        if(employee.getShop_id() != null){
            query.setShopId(employee.getShop_id());
        }
        return super.queryPage(query);

    }
//    private PetAcquisitionOrder pet2order(Pet pet, SearchMasterMsg adopt) {
//        PetAcquisitionOrder order = new PetAcquisitionOrder();
//        order.setDigest("[摘要]对"+pet.getName()+"收购订单！");
//        order.setState(0);//待支付
//        order.setPrice(pet.getCostprice());
//        order.setAddress(adopt.getAddress());
//        String orderSn = CodeGenerateUtils.generateOrderSn(adopt.getUser_id());
//        order.setOrderSn(orderSn);
//        order.setPet_id(pet.getId());
//        order.setUser_id(adopt.getUser_id());
//        order.setPaytype(0);
//        order.setShop_id(pet.getShop_id());
//        order.setEmp_id(EmployeeContext.getLoginEployee().getId());
//        return order;
//    }
}
